package com.ksyzt.ksite2.client.picturecenter;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RequiresResize;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.Widget;
import com.ksyzt.gwt.client.common.FlashMessageComposite;
import com.ksyzt.gwt.client.common.GwtUtil;
import com.ksyzt.gwt.client.event.MessageEvent;
import com.ksyzt.gwt.client.ui.richeditor.SimpleClickEditor;
import com.ksyzt.ksite2.client.common.ServerProxy;
import com.ksyzt.ksite2.shared.OrgType;
import com.ksyzt.ksite2.shared.SysConst;
import com.ksyzt.ksite2.shared.modle.COLUMNObj;

public class NodeEditor extends FlashMessageComposite
{

    private static NodeEditorUiBinder uiBinder = GWT
	    .create(NodeEditorUiBinder.class);

    interface NodeEditorUiBinder extends UiBinder<Widget, NodeEditor>
    {
    }

    public NodeEditor()
    {
	initWidget(uiBinder.createAndBindUi(this));
    }

    /**
     * 确定删除按钮可用
     */
    void sureCanDelete()
    {
	if (m_candelete && m_org != null)
	{
	    btnDelete.setEnabled(true);
	} else
	{
	    btnDelete.setEnabled(false);
	}
    }

    @UiField
    TextBox txtName;
    @UiField
    TextBox txtRank;
    @UiField
    SimpleClickEditor txtDesc;

    @UiField
    Label lbID;
    @UiField
    Button btnSave;
    @UiField
    Button btnCreateChild;

    // ////////////////////////////////////////////////////////消息显示
    @UiField
    Label lbMessage;

    // ////////////////////////////////////////////////////////删除节点
    @UiField
    Button btnDelete;
    boolean m_onlyone;
    private AsyncCallback<Boolean> on_delete_handler = new AsyncCallback<Boolean>()
    {

	public void onSuccess(Boolean result)
	{

	    // initData(m_userid, m_orgtype, m_onlyone);
	    edit(m_userid, m_orgtype, null, null, null);
	    sureCanDelete();
	    message("删除成功");
	}

	public void onFailure(Throwable caught)
	{
	    message(caught.getMessage());
	}
    };

    @UiHandler("btnDelete")
    void onDelete(ClickEvent e)
    {
	if (m_org != null && m_org.getID() != null)
	{
	    ServerProxy.SYS_MANAGER.deleteOrganization(m_org.getID(),
		    on_delete_handler);
	}

    }

    public void fireUpdate()
    {
	MessageEvent e = new MessageEvent(MessageEvent.UPDATE, 0);
	fireEvent(e);
    }

    @UiField
    HTMLPanel placeholder;

    @Override
    protected void onSaveImage(String json)
    {

	String[] r = GwtUtil.parsePostImageResult(json);
	String rcode = r[0];
	String message = r[1];
	if (rcode.equals("1"))
	{
	    message(SysConst.MSG_UPLOAD_IMAGE_SUCCESS);
	    if (m_org != null)
	    {
		m_org.setICON(message);
		ServerProxy.SYS_MANAGER.modifyColumn(m_org, m_modify_handler);
	    }
	    fireUpdate();
	} else
	{
	    message(message);
	}
    }

    @Override
    protected void onShowFlash()
    {
	int left = placeholder.getAbsoluteLeft() + 10;
	int top = placeholder.getAbsoluteTop() + 10;
	moveFlashTo(left, top);
    }

    final public static String tag = "NodeEditor";
    final public static String strImagepath = "images/column";

    int m_userid;
    int m_orgtype;
    TreeItem m_treeitem;
    COLUMNObj m_org;
    COLUMNObj m_parent;

    @UiField
    Button btnEditPicture;

    @UiHandler("btnEditPicture")
    void onEditPicture(ClickEvent e)
    {
	MessageEvent ev = new MessageEvent(MessageEvent.EDITCONTENT, m_org);
	fireEvent(ev);
    }

    /**
     * 编辑组织树
     * 
     * @param userid
     *            组织所属用户
     * @param orgtype
     *            组织类型
     * @param treeitem
     *            组织树节点
     * @param parent
     *            父节点
     * @param org
     *            当前编辑的组织
     */
    public void edit(int userid, int orgtype, TreeItem treeitem,
	    COLUMNObj parent, COLUMNObj org)
    {
	message("");
	showflash();
	m_orgtype = orgtype;
	m_userid = userid;
	m_treeitem = treeitem;
	m_org = org;
	m_parent = parent;

	if (m_org != null)
	{
	    txtName.setValue(m_org.getNAME());
	    txtRank.setValue(m_org.getRANKVAL().toString());

	    txtDesc.setValue(m_org.getDESCRIPTION());

	    btnCreateChild.setEnabled(true);
	    btnSave.setEnabled(true);
	    lbID.setText("编辑组织架构节点:" + m_org.getID());

	    sureCanDelete();

	    showPicture(strImagepath, m_org.getID());

	    btnEditPicture.setEnabled(true);

	} else
	{
	    showPicture(strImagepath, null);
	    txtName.setValue("");
	    txtRank.setValue("0");

	    txtDesc.setValue("");

	    btnCreateChild.setEnabled(false);
	    btnSave.setEnabled(true);

	    lbID.setText("新建" + OrgType.getOrgNameByType(m_orgtype));

	    btnEditPicture.setEnabled(false);
	}

    }

    @UiHandler("btnCreateChild")
    void onCreateChild(ClickEvent e)
    {
	edit(m_userid, m_orgtype, null, m_org, null);
    }

    @UiHandler("btnSave")
    void onSave(ClickEvent e)
    {
	if (m_org == null)
	{
	    COLUMNObj o = new COLUMNObj();

	    if (m_parent == null)
	    {
		o.setNAME(txtName.getValue());

		o.setDESCRIPTION(txtDesc.getValue());
		o.setRANKVAL(Integer.valueOf(txtRank.getValue()));
		o.setPID(-1);
		o.setOTYPE(m_orgtype);
		o.setROOTID(-1);
		o.setWHOID(m_userid);

		ServerProxy.SYS_MANAGER.modifyColumn(o, m_modify_handler);
	    } else
	    {
		// o.setTAG(txtTag.getValue());
		o.setNAME(txtName.getValue());
		// o.setURL(txtURL.getValue());
		o.setDESCRIPTION(txtDesc.getValue());
		o.setRANKVAL(Integer.valueOf(txtRank.getValue()));
		o.setPID(m_parent.getID());
		o.setOTYPE(m_orgtype);
		// o.setCODE(txtCode.getValue());
		// o.setDISPLAY(checkDisply.getValue() == true ? 1 : 0);
		// o.setUSERVALUE(txtBBSID.getValue());
		// o.setCSS(txtCSS.getValue());
		if (m_parent.getROOTID() == -1)
		{
		    o.setROOTID(m_parent.getID());
		} else
		{
		    o.setROOTID(m_parent.getROOTID());
		}

		o.setWHOID(m_userid);
		ServerProxy.SYS_MANAGER.modifyColumn(o, m_modify_handler);
	    }
	} else
	{
	    // m_org.setTAG(txtTag.getValue());
	    m_org.setNAME(txtName.getValue());
	    // m_org.setURL(txtURL.getValue());
	    m_org.setDESCRIPTION(txtDesc.getValue());
	    m_org.setRANKVAL(Integer.valueOf(txtRank.getValue()));
	    // m_org.setCODE(txtCode.getValue());
	    // m_org.setDISPLAY(checkDisply.getValue() == true ? 1 : 0);
	    // m_org.setUSERVALUE(txtBBSID.getValue());
	    // m_org.setCSS(txtCSS.getValue());
	    ServerProxy.SYS_MANAGER.modifyColumn(m_org, m_modify_handler);
	}
    }

    AsyncCallback<COLUMNObj> m_modify_handler = new AsyncCallback<COLUMNObj>()
    {

	public void onSuccess(COLUMNObj result)
	{
	    fireUpdate();
	}

	public void onFailure(Throwable caught)
	{

	    Window.alert(caught.getMessage());
	}
    };

    private boolean m_candelete = false;

    public void EnabledDelete(Boolean b)
    {
	m_candelete = b;
	sureCanDelete();
    }

    @Override
    protected void onAttach()
    {
	super.onAttach();
	showflash();
    }
}
